package com.yjf.ljr.parser;

import java.util.HashMap;
import java.util.Iterator;

import com.yjf.ljr.common.CommonData;

public class RequestBuilder {

	/**
	 * 构造请求的网址
	 * 
	 * @param reqObj
	 *            请求参数
	 * @return 构造完成的Url
	 */
	public static String build(RequestObject reqObj, String url) {
		StringBuffer sb = new StringBuffer();

		if (reqObj.map != null) {
			if (reqObj.format != null) {
				reqObj.map.put("format", reqObj.format);
			}
			Iterator<String> it = reqObj.map.keySet().iterator();
			while (it.hasNext()) {
				sb.append("&");
				String key = it.next();
				String value = "" + reqObj.map.get(key);
				sb.append(key);
				sb.append("=");
				sb.append(value);
			}
		}

		String params = sb.toString();
		if (null != params && params.startsWith("&")) {
			params = params.substring(1);
		}
		return url + reqObj.method + ".htm?" + params;
	}

	public static String build(RequestObject reqObj) {
		return build(reqObj, CommonData.URL);
	}

	public static class RequestObject {
		public String appkey;
		public String method;
		public String format;
		public HashMap<String, Object> map = new HashMap<String, Object>();
	}
}